<?php

namespace App\Exceptions;

use App\Common\statement\ResponseStatement;
use App\Exceptions\api\BaseException;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
//        return parent::render($request, $exception);

        if (
            $request->server()['HTTP_HOST'] !== config('app.API_DOMAIN')

        ){
            goto parent;
        }

        // 客户数据验证异常
        if ($exception instanceof ValidationException) {
            return $this->exceptionResponse(
                ResponseStatement::HTTP_ERROR,
                $this->formatErrorsFirst($exception->errors())
            );
        }

        if ($exception instanceof ModelNotFoundException) {
            return $this->exceptionResponse(
                ResponseStatement::HTTP_ERROR,
                "非法请求参数!"
            );
        }

        //404
        if ($exception instanceof NotFoundHttpException) {
            return $this->exceptionResponse(
                ResponseStatement::NOT_FOUND,
                ResponseStatement::$codeMessage[ResponseStatement::NOT_FOUND]
            );
        }

        if ($exception instanceof MethodNotAllowedHttpException) {
            return $this->exceptionResponse(
                ResponseStatement::HTTP_ERROR,
            "请求方式错误"
            );
        }

        if ($exception instanceof RouteNotFoundException) {
            return $this->exceptionResponse(
                ResponseStatement::NOT_FOUND,
                ResponseStatement::$codeMessage[ResponseStatement::NOT_FOUND]
            );
        }

        //登录过期
        if ($exception instanceof AuthenticationException) {
            return $this->exceptionResponse(
                ResponseStatement::TOKEN_TIME_OUT,
                ResponseStatement::$codeMessage[ResponseStatement::TOKEN_TIME_OUT]
            );
        }

        if ($exception instanceof UnauthorizedHttpException) {
            return $this->exceptionResponse(
                ResponseStatement::TOKEN_TIME_OUT,
                ResponseStatement::$codeMessage[ResponseStatement::TOKEN_TIME_OUT]
            );
        }

        if ($exception instanceof BaseException) {
            return $this->exceptionResponse(ResponseStatement::HTTP_ERROR,$exception->getMessage());
        }
        if ($exception instanceof \Exception) {
            return $this->exceptionResponse(ResponseStatement::HTTP_ERROR,$exception->getMessage());
        }

        parent:
        return parent::render($request, $exception);
    }

    private function formatErrorsFirst(array $errors)
    {
        return array_first(array_first($errors));
    }

    private function exceptionResponse($code,$msg)
    {
        return response([
            'code' => $code,
            'message' => $msg
        ])->header('Content-Type', 'application/json');
    }

}
